package team.szm.scd.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import team.szm.scd.database.dto.RequestWrapper;
import team.szm.scd.database.dto.Result;
import team.szm.scd.database.entity.Doctor;
import team.szm.scd.database.entity.Topic;
import team.szm.scd.database.entity.TopicReply;
import team.szm.scd.database.entity.User;
import team.szm.scd.service.interfaces.ILoginService;
import team.szm.scd.service.interfaces.ITopicService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/topic")
@Slf4j
public class TopicController {
    @Resource
    ITopicService service;
    @Resource
    ILoginService loginService;

    @PostMapping("/create")
    @ResponseBody
    public Result<Boolean> createTopic(@RequestBody Topic topic, HttpSession session) {
        User user = loginService.getLoginUser(session);
        if (user == null) {
            return Result.needLogin();
        }
        topic.setUserId(user.getId());
        return Result.success(service.createTopic(topic, LocalDateTime.now()));
    }

    @PostMapping("/id")
    @ResponseBody
    public Result<Topic> topicFromId(@RequestBody RequestWrapper<String> id, HttpSession session) {
        if (loginService.noUserAndNoDoctorLogin(session)) {
            return Result.needLogin();
        }
        return Result.success(service.fromId(id.value));
    }

    @PostMapping("/reply")
    @ResponseBody
    public Result<Boolean> replyTopic(@RequestBody TopicReply reply, HttpSession session) {
        Doctor doctor = loginService.getLoginDoctor(session);
        if (doctor == null) {
            return Result.needLogin();
        }
        reply.setDoctorId(doctor.getId());
        return Result.success(service.replyTopic(reply, LocalDateTime.now()));
    }

    @Data
    private static class TopicRequestWrapper {
        String searchInfo;
        Integer pageNum;
    }

    @PostMapping("/all_topics")
    @ResponseBody
    public Result<Page<Topic>> getAllTopics(@RequestBody TopicRequestWrapper wrapper,
                                            HttpSession session) {
        log.trace(wrapper.toString());
        if (loginService.noUserAndNoDoctorLogin(session)) {
            return Result.needLogin();
        }
        return Result.success(service.getAllTopics(wrapper.pageNum, wrapper.searchInfo));
    }

    @Data
    private static class ReplyRequestWrapper {
        String topicId;
        Integer pageNum;
    }

    @PostMapping("/all_replies")
    @ResponseBody
    public Result<Page<TopicReply>> getAllReplies(@RequestBody ReplyRequestWrapper wrapper,
                                                  HttpSession session) {
        if (loginService.noUserAndNoDoctorLogin(session)) {
            return Result.needLogin();
        }
        return Result.success(service.getAllReplies(wrapper.pageNum, wrapper.topicId));
    }
}
